package aos.client.gui;

/* Copyright 2008 Robert Bajzat. All rights reserved. GPL v3. Use is subject to license terms. */


import java.awt.*;
import java.awt.event.*;

import thinlet.Slider;
import thinlet.Widget;
import thinlet.event.*;
import thinlet.layout.*;

public class VerticalSlider extends Slider {
	
	public VerticalSlider() {
	}
	
	public Metrics getPreferredSize(int preferredWidth) {
		return new Metrics(14, 14);
	}
	
	protected void process(AWTEvent e) {
		switch (e.getID()) {
		case MouseEvent.MOUSE_ENTERED:
		case MouseEvent.MOUSE_EXITED:
			repaint();
			break;
		case MouseEvent.MOUSE_PRESSED:
			requestFocus();
		case MouseEvent.MOUSE_DRAGGED:
			setValue((((MouseEvent) e).getY() - 6 + 0.5f) / (getHeight() - 12));
			repaint();
			break;
		case MouseEvent.MOUSE_RELEASED:
			repaint();
			break;
		case KeyEvent.KEY_PRESSED:
			KeyEvent ke = (KeyEvent) e;
			int code = ke.getKeyCode();
			if (code == KeyEvent.VK_HOME) {
				setValue(1);
			}
			else if ((code == KeyEvent.VK_LEFT) || (code == KeyEvent.VK_UP)) {
				setValue(getValue() - 0.05f);
			}
			else if (code == KeyEvent.VK_PAGE_UP) {
				setValue(getValue() - 0.25f);
			}
			else if (code == KeyEvent.VK_PAGE_DOWN) {
				setValue(getValue() + 0.25f);
			}
			else if ((code == KeyEvent.VK_RIGHT) || (code == KeyEvent.VK_DOWN)) {
				setValue(getValue() + 0.05f);
			}
			else if (code == KeyEvent.VK_END) {
				setValue(0);
			}
			break;
		case FocusEvent.FOCUS_GAINED:
		case FocusEvent.FOCUS_LOST:
			repaint();
		}
	}
	
	protected void paint(Graphics2D g) {
		int h = getHeight(), length = (int) (getValue() * (h - 14) + 0.5f);
		
		g.setPaint(new GradientPaint(
			0, 3, new Color(0xc0c0c0), 0, 9, new Color(0xe0e0e0)));
		g.fillRoundRect(4, 1, 6, h - 2, 4, 4);
		g.setColor(new Color(0xa0a0a0));
		g.drawRoundRect(3, 0, 7, h - 1, 6, 6);
		
		boolean enabled = true, focused = isFocused(),  inside = isMouseInside(), pressed = isPressedInside();
		g.setColor(enabled ? focused ? new Color(0x3070a0) : Color.gray : new Color(0xa0a0a0));
		g.fillOval(0, length, 14, 14);
		
		int i = focused ? 2 : 1;
		g.setPaint(new GradientPaint(i, 0, enabled ? inside ? pressed ? Color.gray/*pressed*/ :
			Color.white/*hover*/ : Color.white/*normal*/ : Color.white/*disabled*/,
		14 - 2 * i - 1, 0, enabled ? inside ? pressed ? new Color(0xa0a0a0)/*pressed*/ :
			new Color(0xd0d0d0)/*hover*/ : new Color(0xb0b0b0)/*normal*/ : new Color(0xd0d0d0)/*disabled*/));
		g.fillOval(i, length + i, 14 - 2 * i, 14 - 2 * i);
	}
}
